//
//  ReturnEvaluateTableViewController.m
//  HaiGoumerMerchant
//
//  Created by apple on 17/1/19.
//  Copyright © 2017年 com.zaoguankeji.www. All rights reserved.
//

#import "ReturnEvaluateTableViewController.h"

#import "ReturnEvaluateTableViewCell.h"
static   NSString  *cellID = @"ReturnEvaluateTableViewCell";

@interface ReturnEvaluateTableViewController ()<UITableViewDelegate, UITableViewDataSource ,UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic, strong) UITextField *textField;
@end
@implementation ReturnEvaluateTableViewController
{
    
    CGFloat _lastScrollViewOffsetY;
    CGFloat _totalKeybordHeight;
}
static CGFloat textFieldH = 40;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureTableView];
     [self setupTextField];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardNotification:) name:UIKeyboardWillChangeFrameNotification object:nil];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
 
}
- (void)setupTextField  {
    _textField = [UITextField new];
    _textField.returnKeyType = UIReturnKeyDone;
    _textField.delegate = self;
    _textField.layer.borderColor = [[UIColor lightGrayColor] colorWithAlphaComponent:0.8].CGColor;
    _textField.layer.borderWidth = 1;
    _textField.frame = CGRectMake(0, self.view.bounds.size.height- 40 , self.view.width, textFieldH);
    [[UIApplication sharedApplication].keyWindow addSubview:_textField];
    
//    [_textField becomeFirstResponder];
    
}
- (void)configureTableView {
    self.title = @"点评详情";
    self.myTableView.rowHeight = UITableViewAutomaticDimension;
    self.myTableView.estimatedRowHeight = 130;
    self.myTableView.tableFooterView = [UIView new];
    self.myTableView.showsVerticalScrollIndicator = NO;
    self.myTableView.showsHorizontalScrollIndicator =NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ReturnEvaluateTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [_textField resignFirstResponder];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [_textField resignFirstResponder];
    _textField.placeholder = nil;
}
- (void)keyboardNotification:(NSNotification *)notification
{
        NSDictionary *dict = notification.userInfo;
        CGRect rect = [dict[@"UIKeyboardFrameEndUserInfoKey"] CGRectValue];
    
    
    
        CGRect textFieldRect = CGRectMake(0, rect.origin.y - textFieldH, rect.size.width, textFieldH);
        if (rect.origin.y == self.view.bounds.size.height - 40) {
            textFieldRect = rect;
        }
    
        [UIView animateWithDuration:0.25 animations:^{
            _textField.frame = textFieldRect;
        }];
    
        CGFloat h = rect.size.height + textFieldH;
        if (_totalKeybordHeight != h) {
            _totalKeybordHeight = h;
        }
}
- (void)dealloc
{
       
    [_textField removeFromSuperview];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)adjustTableViewToFitKeyboard
{
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    UITableViewCell *cell = [self.myTableView cellForRowAtIndexPath:_currentEditingIndexthPath];
//    CGRect rect = [cell.superview convertRect:cell.frame toView:window];
//    [self adjustTableViewToFitKeyboardWithRect:rect];
}

//- (void)adjustTableViewToFitKeyboardWithRect:(CGRect)rect
//{
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    CGFloat delta = CGRectGetMaxY(rect) - (window.bounds.size.height - _totalKeybordHeight);
//    
//    CGPoint offset = self.myTableView.contentOffset;
//    offset.y += delta;
//    if (offset.y < 0) {
//        offset.y = 0;
//    }
//    
//    [self.myTableView setContentOffset:offset animated:YES];
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
//
//    return 300;
//}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
